﻿
using EvaluationSystem.Models;
using EvaluationSystem.Models.Views;
using EvaluationSystem.ViewModels;

namespace EvaluationSystem.Utils
{
    public class ScoreUtil
    {
        /// <summary>
        /// 获取表格数据
        /// </summary>
        /// <param name="ec_list"></param>
        /// <returns></returns>
        public static Tuple<List<string>, List<float>>? getChartData(IList<Eval_Course>? ec_list)
        {
            if (ec_list == null || ec_list.Count < 1)
            {
                return default;
            }

            var teacher_array = new List<string>();
            var score_array = new List<float>();

            var score_dict = new Dictionary<string, float>(); // 老师的平均分
            var s_dict = new Dictionary<string, int>(); // 老师的学生数量

            // 先算老师下的学生总评分,等于每个学生的评分的和
            foreach (Eval_Course? item in ec_list)
            {
                var tname = item.tname; // 老师名称
                var evals = item.Evals; // 评分结果
                if (!string.IsNullOrEmpty(tname))
                {
                    // 获取评分分数
                    var score = getScore(evals); // 不再手动计算, 直接使用表里计算的值
                    // 存入字典,后续计算平均分
                    if (score_dict.Any(t => t.Key == tname))
                    {
                        score_dict[tname] += score;
                    }
                    else
                    {
                        score_dict.Add(tname, score);
                        s_dict.Add(tname, 0);
                    }
                    s_dict[tname]++;
                }
            }

            // 求老师的平均分
            foreach (var tname in score_dict.Keys)
            {
                // 学生数量
                var scount = s_dict[tname];
                teacher_array.Add(tname);
                if (scount >= 2)
                {
                    var score = score_dict[tname] / scount;
                    score = float.Parse(score.ToString("F2"));
                    score_array.Add(score);
                }
                else
                {
                    score_array.Add(score_dict[tname]);
                }
            }
            return Tuple.Create(teacher_array, score_array);
        }


        /// <summary>
        /// 获取表格数据
        /// </summary>
        /// <param name="sc_list"></param>
        /// <returns></returns>
        public static Tuple<List<string>, List<float>>? getChartData2(List<StudentEvalCourse>? sc_list)
        {
            if (sc_list == null || sc_list.Count < 1)
            {
                return default;
            }

            var teacher_array = new List<string>();
            var score_array = new List<float>();

            var score_dict = new Dictionary<string, float>(); // 老师的平均分
            var s_dict = new Dictionary<string, int>(); // 老师的学生数量

            // 先算老师下的学生总评分,等于每个学生的评分的和
            foreach (var item in sc_list)
            {
                var tname = item.TeacherWithCourses?.Teacher?.Name; // 老师名称
                var evals = item.Evals; // 评分结果
                if (!string.IsNullOrEmpty(tname))
                {
                    // 获取评分分数
                    var score = item.Score;
                    if (score == null)
                        continue;
                    // 存入字典,后续计算平均分
                    if (score_dict.Any(t => t.Key == tname))
                    {
                        score_dict[tname] += score!.Value;
                    }
                    else
                    {
                        score_dict.Add(tname, score!.Value);
                        s_dict.Add(tname, 0);
                    }
                    s_dict[tname]++;
                }
            }

            // 求老师的平均分
            foreach (var tname in score_dict.Keys)
            {
                // 学生数量
                var scount = s_dict[tname];
                teacher_array.Add(tname);
                if (scount >= 2)
                {
                    var score = score_dict[tname] / scount;
                    score = float.Parse(score.ToString("F2"));
                    score_array.Add(score);
                }
                else
                {
                    score_array.Add(score_dict[tname]);
                }
            }
            return Tuple.Create(teacher_array, score_array);
        }

        /// <summary>
        /// 获取评价的题目和结果
        /// </summary>
        /// <param name="text"></param>
        /// <param name="ques"></param>
        /// <returns></returns>
        public static List<QuesInfoVM> GetQuesInfos(string? text, List<Claim>? ques)
        {
            // 切割问题信息
            var qinfos = new List<QuesInfoVM>();
            if (!string.IsNullOrEmpty(text))
            {
                var list = text.Split(",", StringSplitOptions.RemoveEmptyEntries);
                var dict = new Dictionary<int, int>();
                foreach (var item in list)
                {
                    int.TryParse(item.Split("_")[0], out int cid);
                    int.TryParse(item.Split("_")[1], out int qid);
                    if (cid > 0 && qid > 0)
                        dict.Add(cid, qid);
                }

                var cids = dict.Keys;
                // 获取问题对照表
                //List<Claim>? ques = _dbContext.Claim.Where(x => cids.Contains(x.Id)).ToList();
                if (ques != null)
                    foreach (var item in ques)
                    {
                        qinfos.Add(new QuesInfoVM
                        {
                            Claim = item,
                            Answer = dict[item.Id]
                        });
                    }
            }

            return qinfos;
        }


        /// <summary>
        /// 评分
        /// </summary>
        /// <param name="evals"></param>
        /// <returns></returns>
        public static float getScore(string evals)
        {
            float score = 0;
            if (string.IsNullOrWhiteSpace(evals))
                return score;

            var vals = evals.Split(',');
            foreach (var kv in vals)
            {
                var val = kv.Split('_');
                if (val.Length == 2)
                {
                    score += mapScore(val[1]);
                }
            }
            return score;
        }

        /// <summary>
        /// 评分
        /// </summary>
        /// <param name="studentEvals"></param>
        /// <returns></returns>
        public static float getScore(IList<StudentEvalCourse> studentEvals)
        {
            float score = 0;
            float studentCount = 0;
            foreach (var item in studentEvals)
            {
                var evals = item.Evals;
                var vals = evals?.Split(',');
                if (vals != null)
                    foreach (var kv in vals)
                    {
                        var val = kv.Split(',');
                        if (val.Length == 2)
                        {
                            score += mapScore(val[1]);
                        }
                    }

                studentCount++;
            }
            return score == 0 ? 0 : score / studentCount;
        }

        /// <summary>
        /// 分数映射
        /// </summary>
        /// <param name="val"></param>
        /// <returns></returns>
        public static float mapScore(string val)
        {
            float value = 0;
            if (float.TryParse(val, out var s))
            {
                value = s switch
                {
                    1 => 4,
                    2 => 3,
                    3 => 2,
                    4 => 1,
                    _ => 0
                };
            }
            return value;
        }
    }
}
